package com.mililoan.console.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.mililoan.common.bean.UserStatus;
import com.mililoan.common.exception.SessionTimeoutException;
import com.mililoan.model.system.User;
import com.mililoan.service.module.system.UserBo;

/**
 * 在线用户处理
 * 
 * @author hozen 2014-6-26
 */
public class OnlineService {
	// private static HashMap statusHm = new HashMap();

	private static HashMap	statusHm	= new HashMap();;
	private static List		statusList	= new ArrayList();;

	@Autowired
	private static UserBo	userBo;

	/**
	 * 读取所有状态的HashMap列表
	 * 
	 * @since hozen 2014-6-26
	 * @return
	 */
	public synchronized static HashMap getStatusHm() {
		return statusHm;
	}

	/**
	 * 读取所有状态的List列表
	 * 
	 * @since hozen 2014-6-26
	 * @return
	 */
	public synchronized static List getStatusList() {
		return statusList;
	}

	/**
	 * 更新状态的List列表
	 * 
	 * @since hozen 2014-6-26
	 * @param statusList
	 * @return
	 */
	public static List setStatusList(List ssList) {
		statusList = ssList;
		return statusList;
	}

	/**
	 * 读取状态
	 * 
	 * @since hozen 2014-6-26
	 * @param userid
	 * @return
	 */
	public static UserStatus getStatus(long userid) {

		UserStatus status = (UserStatus) getStatusHm().get(userid + "");

		return status;
	}

	/**
	 * 存状态,updateOld是否更新原数据
	 * 
	 * @since hozen 2014-6-26
	 * @param status
	 * @param updateOld
	 * @return
	 */
	public synchronized static boolean putStatus(UserStatus status, boolean updateOld) {

		UserStatus stt = (UserStatus) getStatusHm().get(status.getUserid() + "");
		if (stt == null) {// 不存在，直接插入
			getStatusHm().put(status.getUserid() + "", status);
			getStatusList().add(status);

		} else {// 存在
			if (updateOld) {// 如果需要更新，则删除原数据
				stt.setDeleted(true);// 设置删除标记
				stt.setLogoutDate(new Date());
				getStatusHm().remove(status.getUserid() + "");
				getStatusHm().put(status.getUserid() + "", status);
				getStatusList().add(status);
			}
		}

		return true;
	}

	/**
	 * 更新状态
	 * 
	 * @since hozen 2014-6-26
	 * @param userid
	 * @return
	 * @throws SessionTimeoutException
	 */
	public static UserStatus updateTouchDate(long userid) throws SessionTimeoutException {

		UserStatus status = (UserStatus) getStatusHm().get(userid + "");
		if (status == null) {

			if (userBo == null) {
				throw new SessionTimeoutException("登录超时");
			}
			User user = userBo.getUserById( userid);

			if (user == null) {
				return null;
			}
			status = new UserStatus();
			status.setUserName(user.getUserName());
			status.setUserid(userid);

			putStatus(status, false);

		}
		status.setTouchDate(new Date());

		return status;
	}

	/**
	 * 删除状态
	 * 
	 * @since hozen 2014-6-26
	 * @param userid
	 * @return
	 */
	public static boolean deleteStatus(long userid) {
		UserStatus status = (UserStatus) getStatus(userid);
		if (status != null) {
			status.setDeleted(true); // 设置删除标记
			status.setLogoutDate(new Date());
			getStatusHm().remove(userid + "");
		}
		return true;
	}

	/**
	 * 
	 * 判断是否当前登录用户
	 * 
	 * @since hozen 2014-6-26
	 * @param userid
	 * @param sessionId
	 * @return
	 */
	public static boolean checkCurrLogin(long userid, String sessionId) {
		UserStatus status = (UserStatus) getStatusHm().get(userid + "");
		if (status != null && status.getSessionId() == sessionId) {
			return true;
		}
		return false;
	}

}
